package xin.mydreams.mybill.widget.home.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import xin.mydreams.mybill.global.result.GetFormatResult;
import xin.mydreams.mybill.global.result.PostFormatResult;
import xin.mydreams.mybill.global.result.exception.MyException;
import xin.mydreams.mybill.global.result.exception.NoLoginException;
import xin.mydreams.mybill.util.CodeUtil;
import xin.mydreams.mybill.util.NetworkUtil;
import xin.mydreams.mybill.widget.home.service.ILoginService;
import xin.mydreams.mybill.widget.user.entity.User;

/**
 * 登录controller
 *
 * @author 谷泽昊
 * @Type LoginController.java
 * @Desc
 * @date 2017年12月7日 上午10:17:53
 */
@RestController
@RequestMapping
public class HomeController {

    @Autowired
    private ILoginService loginService;

    /**
     * 登录
     *
     * @param userName   账号
     * @param password   密码
     * @param rememberMe 记住我 验证码
     * @return
     * @throws MyException
     * @date Created at 2017年11月17日 下午6:58:50
     * @Author 谷泽昊
     */
    @PostFormatResult(value = "/login")
    public Object login(@RequestParam(value = "userName") String userName,
                        @RequestParam(value = "password") String password,
                        @RequestParam(value = "rememberMe", required = false) String rememberMe,
                        // @ApiParam("验证码") @RequestParam(value = "code") String code,
                        HttpServletRequest request, HttpServletResponse response) throws MyException {
        UsernamePasswordToken token = new UsernamePasswordToken(userName, password, rememberMe);
        return loginService.login(token, userName, NetworkUtil.getIpAddress(request));
    }

    /**
     * 查询是否登录
     *
     * @return
     * @throws MyException
     * @throws NoLoginException
     * @date Created at 2019年2月15日 下午9:42:40
     * @mail guzehao@mydreams.xin
     * @author 谷泽昊
     */
    @GetFormatResult(value = "/login")
    public Object login() throws MyException, NoLoginException {
        try {
            Subject currentUser = SecurityUtils.getSubject();
            if (currentUser.isAuthenticated()) {

                Object principal = currentUser.getPrincipal();
                if (principal instanceof User) {
                    return (User) principal;
                }
            }
        } catch (Exception e) {
//			log.error("获取用户失败：" , e);
        }
        throw new NoLoginException("没有登录！");
    }

    /**
     * 权限丢失
     *
     * @return
     * @throws MyException
     * @date Created at 2019年5月27日  下午10:16:37
     * @mail guzehao@mydreams.xin
     * @author 谷泽昊
     */
    @GetFormatResult(value = "/forbidden")
    public Object forbidden() throws MyException {
        throw new MyException(CodeUtil.FORBIDDEN_FAIL, "没有权限！");
    }

    /**
     * 退出
     *
     * @return
     * @throws MyException
     * @throws NoLoginException
     */
    @GetFormatResult(value = "/logout")
    public Object logout() throws MyException, NoLoginException {
        try {
            Subject currentUser = SecurityUtils.getSubject();
            if (currentUser.isAuthenticated()) {
                currentUser.logout();
            }
        } catch (Exception e) {
//			log.error("获取用户失败：" , e);
        }
        throw new NoLoginException("没有登录！");
    }
}
